<template>
  <div class="cate">
    <van-search @click="$router.push('/search')"  v-model="value" shape="round" placeholder="搜索鲜花、蛋糕、礼品" />
    <div class="cates">
      <div class="cate-nav">
        <van-sidebar v-model="active">
          <van-sidebar-item @click="switchTab(index)"
          v-for="(cate,index) in cates" :key="cate.id"
           :title="cate.name" />
        </van-sidebar>
      </div>
      <div class="cate-list" v-if="!loading"  >
        <div class="items">
          <van-empty description="暂无商品" v-if="itemShow" />
          <div class="item"  @click="enterDetail(item.id)" v-for="item in items" :key="item.id">
            <div class="icon">
              <img :src="cates.icon" alt="">
            </div>
            <div class="item-left">
              <img :src="item.pic" alt />
            </div>
            <div class="item-right">
              <h3>{{ item.name }}</h3>
              <p>{{ item.characteristic ? item.characteristic: '暂无描述' | filterDesc}}</p>
              <div class="pic">
                <div class="pic-left">
                  <h3>{{ item.minPrice | currency }}</h3>
                  <span>已销售13.6万</span>
                </div>
                <div class="pic-right">
                  <van-icon @click.stop="addCart(item.id)" name="cart-circle" />
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <tabbar />
  </div>
</template>

<script>
// 引入公共组件tabbar
import Tabbar from '@components/tabbar'
import { fetchCates, fetchHomeList, addCart} from '@api/index'
import { isLogin } from '@utils'
import { Toast } from 'vant'
export default {
  // 放置到局部组件中使用
  components: {
    Tabbar
  },
  data () {
    return {
      active: 0,
      cates: [], // 请求的分类信息
      value: '', // 绑定的搜索框
      items: [], // 分类的商品数据
      loading: true
    }
  },
  filters: {
    filterDesc (v, len = 10) {
      if (v.length > 10) {
        return v.substr(0, len) + '...'
      } else {
        return v
      }
    },
    currency (v, sign = '￥') {
      return sign + v
    }
  },
  computed: {
    // 这个是判断这个类别有没有商品
    itemShow () {
      return !this.items.length
    }
  },
  created () {
    this.fetchCates()
  },
  methods: {
    addCart (id) {
      // 先做判断是否登录 登录就加入购物车 没有登录就去登录页
      if (isLogin()) {
        // 已登录 可以添加到购物车
        addCart({
        // 后端接受的参数 传入过去
          goodsId: id,
          number: 1,
          token: localStorage.getItem('token') || ''
        }).then(res => {
          if (res.data.code === 0) {
            // 添加成功后从新给state里的数据赋值
            this.$store.commit('cart/init_cart_info', {
              items: res.data.data.items,
              todal: res.data.data.price
            })
            Toast.success({
              message: '加入成功',
              duration: 1000
            })
          }
        })
      } else {
        // 没有的登录 给一个提示 然后跳转到登录页
        Toast.fail({
          message: '暂无登录',
          duration: 1000,
          onClose: () => {
            // 这里跳转到登录页面
            this.$router.push({
              path: '/login',
              query: {
                from: '/cate'
              }

            })
          }
        })
      }
    },
    // 点击进入详情页
    enterDetail (id) {
      this.$router.push(`/itemDetail/${id}`)
    },
    // 点击切换分类执行的函数
    switchTab (index) {
      this.loading = true
      this.active = index // 传过来的下标 然后就让他等于active达到active效果的切换
      this.fetchItems() // 然后重新调用请求商品的函数
    },
    // 请求所有分类
    fetchCates () {
      fetchCates().then((res) => {
        if (res.data.code === 0) {
          this.cates = res.data.data
          this.fetchItems() // 请求默认的商品
        }
      })
    },
    // 分类下的商品
    fetchItems () {
      fetchHomeList({
        categoryId: this.cates[this.active].id
      }).then((res) => {
        this.loading = false
        this.items = []
        if (res.data.code === 0) {
          this.items = res.data.data.result
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.cate {
  background-color: #fff;
  .van-search {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
  }
  .cates {
    display: flex;
    position: fixed;
    top: 54px;
    left: 0;
    right: 0;
    bottom: 50px;
    .van-empty{
      margin: 50px;
    }
    .cate-nav {
      width: 22%;
       overflow-y: auto;
    }
    .cate-list {
      flex: 1;
      overflow-y: auto;
    }
  }
}
.item {
  background-color: #fff;
  display: flex;
  margin-bottom: 10px;
  .item-left {
    img {
      width: 170px;
      height: 186px;
    }
  }
  .item-right {
    width: 100%;
    box-sizing: border-box;
    padding: 12px 0 8px 12px;
    h3 {
      font-weight: 600;
    }
    p {
      font-size: 13px;
      line-height: 18px;
    }
    .pic {
      margin-top: 80px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding-right: 10px;
      box-sizing: border-box;
      .pic-left {
        span {
          font-size: 12px;
          color: #71797f;
        }
      }
      .pic-right {
        font-size: 22px;
        color: rgb(255, 130, 100);
      }
    }
  }
}
</style>
